<?php

namespace Frieza\Modules\AdServing\Controllers;

use Encore\Admin\Grid;
use Illuminate\Http\Request;
use Encore\Admin\Widgets\Tab;
use App\Pockets\GridDataModel;
use Encore\Admin\Widgets\Form;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Doraemon\model\AdServing\UploadApkLog;

/**
 * Description: 上传日志
 * Created by PhpStorm.
 * User: 玉坤
 */
class UploadLogController extends Controller
{

    /**
     * 上传apk日志列表
     *
     * @param Content $content
     * @param Request $request
     * @return Content
     */
    public function index(Content $content, Request $request): Content
    {
        $params = $request->all();
        $page_size = $params['per_page'] ?? 20;
        $page = $params['page'] ?? 1;
        $where['LIMIT'] = [
            ($page - 1) * $page_size,
            $page_size,
        ];
        $where['ORDER'] = [
            'id' => 'DESC',
        ];
        $list = UploadApkLog::all($where);
        unset($where['LIMIT']);
        $count = UploadApkLog::count($where);
        $gridModel = new GridDataModel($list, $count, [
            'perPage' => $page_size,
        ]);
        $grid = Admin::grid($gridModel,
            function (Grid $grid) use ($gridModel) {
                $grid->paginate($gridModel->getPerPage());
                $grid->disableFilter();
                $grid->disableExport();
                $grid->disableRowSelector();
                $grid->disableCreateButton();
                $grid->column('id', 'id');
                $grid->column('version', '版本号')->copyable();
                $grid->column('is_beta', '类别')->using([
                    1 => '灰度版',
                    2 => '正式版',
                ]);
//                $grid->column('apk_type', '类别')->using([
//                    1 => '安卓',
//                    2 => 'IOS',
//                ]);
                $grid->column('download', 'beta包下载')->display(function () {
                    if (!$this->version || $this->is_beta != 1) {
                        return '';
                    }
                    $url = 'https://d.hhz1.cn/beta/' . $this->version . '/hhz.apk';
                    return '<a href="' . $url . '" target="_blank">安卓beta-' . $this->version . '</a>';
                });
                $grid->column('admin_name', '操作人');
                $grid->column('create_time', '时间');
                $grid->disableActions();
            }
        );
        $tab = new Tab();
        $tab->addLink("灰度Apk上传", "/admin/adServing/uploadLog/show");
        $tab->add('App上传记录', $grid->render(), true);
        $tab->addLink('正式Apk上传', '/admin/adServing/upload');
        $content->row($tab);

        return $content
            ->header('Android包上传记录')
            ->description(' ')
            ->breadcrumb(['text' => 'Android包上传', 'url' => '/adServing/upload']);
    }


    public function show(Content $content): Content
    {
        $baseForm = new Form();
        $baseForm->hidden('is_beta')->default(1);
        $baseForm->text('version', '灰度版本号')->required()->help("例：1.1.0");
        $baseForm->largefile('apk_file', '上传app包')->rules(['apk_file' => 'mimes:apk'])->required()->help("例：hhz.apk");
        $baseForm->confirm('确定更新灰度包吗？');
        $baseForm->action('/admin/adServing/upload/save');

        $tab = new Tab();
        $tab->add("灰度Apk上传", $baseForm, true);
        $tab->addLink("App上传记录", "/admin/adServing/uploadLog");
        $tab->addLink("正式Apk上传", "/admin/adServing/upload");
        return $content
            ->header('投放管理')
            ->breadcrumb(
                [
                    'text' => '投放渠道列表',
                    'url' => '/adServing/serving'
                ],
                ['text' => "好好测包上传"]
            )
            ->body($tab);
    }

}